package game.engine;

import game.sprite.Sprite;
import java.io.File;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 * 
 * Returns a list of sprites as determined by a level properties file
 * 
 * @author mah75
 * 
 */
public class LevelCreator {

    /**
     * Create the right type of parser based on reflection.
     */
    private LevelParser myLevelParser;

    /**
     * 
     * @param file File to be used during level creation.
     */
    public LevelCreator (File file) {
        myLevelParser = new XmlParser(file);
    }

    /**
     * 
     * @param fileToLoad name of the resource file containing the level
     *        information
     * @return a list of all the sprites in the level
     */
    public static List<Sprite> createLevel (File fileToLoad) {
        return null;
    }

    /**
     *  testing stuff. This demonstrates the ability to parse an XML file.
     *  NodeLists contain the Elements of XML as described there (i.e. Root, sprite).
     *  Each element contains a section of XML and each NodeList contains the
     *  attached qualities of these elements.
     *
     * @param args Arguments.
     */
    public static void main (String[] args) {
        File file = new File("src/resources/example.xml");
        XmlParser parser = new XmlParser(file);

        if (parser.isValid()) {
            Element rootElement = parser.getRootElement();
            NodeList rootElementList = parser.getElementsByName(rootElement, "sprite");

            for (int i = 0; i < rootElementList.getLength(); i++) {

                Element spriteElement = (Element) rootElementList.item(i);
                NodeList spriteList = parser.getElementsByName(spriteElement, "type");

                for (int j = 0; j < spriteList.getLength(); j++) {

                    Element typeElement = (Element) spriteList.item(j);
                    NodeList methodList = typeElement.getElementsByTagName("method");
                    NodeList argumentList = typeElement.getElementsByTagName("argument");
                    String method = methodList.item(0).getTextContent();
                    String argument = argumentList.item(0).getTextContent();
                    parser.reflectMethod(method, argument);
                }
            }
        }
    }
}
